var time = 1e3;

function leftTimer(a, b, c, d, f, g) {
  var h = new Date(a, b - 1, c, d, f, g) - new Date,
    j = parseInt(h / 1e3 / 60 / 60 / 24, 10),
    k = parseInt(h / 1e3 / 60 / 60 % 24, 10),
    l = parseInt(h / 1e3 / 60 % 60, 10),
    m = parseInt(h / 1e3 % 60, 10);
  j = checkTime(j),
    k = checkTime(k),
    l = checkTime(l),
    m = checkTime(m)
}

function mtTimeOut() {
  var a = setTimeout(function () {
    if (time--, 0 < time)
      return leftTimer(2017, 11, 11, 11, 11, 11),
        clearTimeout(a),
        a = null,
        mtTimeOut()
  }, time)
}

function checkTime(a) {
  return 10 > a && (a = "0" + a),
    a
}
var timer = setInterval(function () {
    document
      .getElementById("load_bg_img")
      .complete && ($(".loading").fadeOut(), clearInterval(timer), timer = null)
  }, 1e3),
  page = {
    card_body: ".card-body",
    card_chance: ".card_chance",
    card_chance1: ".card_chance1",
    card_chance2: ".card_chance2",
    card_chance3: ".card_chance3",
    btn_price: "#btn_price",
    btn_medal: "#btn_medal",
    btn_ruler: "#btn_ruler",
    invite_btn: "#invite_btn",
    game_price: "#game_price",
    game_ruler: "#game_ruler",
    game_madel: "#game_madel",
    game_invite: "#game_invite",
    game_result: "#game_result",
    back_main_btn: ".back-main-btn",
    play_invite: ".play-invite",
    btn_close: ".btn-close",
    btn_reload: ".btn-reload",
    btn_play_again: ".play-again",
    times: "#times",
    play_game_times: 0,
    obj: {},
    coupon: null,
    openId: GetRequest().openId,
    host: getServerHost(),
    compareCard: [],
    count: 0,
    youhuiquan: "",
    hasAward: !1,
    clickCount: 0,
    prizeName: [
      "",
      "iPad",
      "\u7FBD\u6CC9\u4E8C\u5341\u5468\u5E74\u6F14\u5531\u4F1A\u95E8\u7968",
      "\u8054\u901A\u9650\u91CF\u7B14\u8BB0\u672C",
      "\u963F\u80F6\u4F18\u60E0\u5238",
      "100M\u7701\u5185\u6D41\u91CF"
    ],
    medal_failed: ".medal-failed",
    medal_success: ".medal-success",
    prizeType: [{
      type: 1,
      imgUrl: "./getImg/1.png"
    }, {
      type: 2,
      imgUrl: "./getImg/2.png"
    }, {
      type: 3,
      imgUrl: "./getImg/3.png"
    }, {
      type: 4,
      imgUrl: "./getImg/4.png"
    }, {
      type: 5,
      imgUrl: "./getImg/5.png"
    }],
    oldList: "",
    cardGame: {
      imgList: [],
      lessNum: 0,
      oldList: [
        1, 2, 3, 4, 5
      ],
      newList: [],
      imgShowList: [],
      cardDesk: [],
      init: function init() {
        this.cardSort(this.oldList, this.newList, this.lessNum),
          page.oldList = this.oldList
      },
      cardSort: function cardSort(a, b, c) {
        var d = this;
        if (a.length != c) {
          var f = Math.floor(Math.random() * a.length);
          return b.push(a[f]),
            a.splice(f, 1),
            d.cardSort(a, b, c)
        }
      },
      createCard: function createCard(a, b) {
        if (b) {
          var c = new Image;
          c.onload = function () {
              c.onload = null,
                a.append(c)
            },
            c.src = b
        }
      },
      getImgList: function getImgList(a, b, c) {
        for (var d = 0; d < a.length; d++)
          c.push(b[a[d]]);
        return c
      }
    },
    init: function init() {
      this;
      this.showPrice(),
        this.pageSwap(),
        this.sendAddr(),
        this.getPrizeFunc(),
        $
        .when(this.checkUserFunc())
        .done(function (b) {
          if (0 == b.code) {
            var c = b.data;
            if (c.offline && (location.href = "./activeDownline.html"), page.play_game_times = c.times, page.hasAward = c.hasAward, page.userPhone = c.phone, page.youhuiquan = c.coupon, $("#youhuiquan").text(page.youhuiquan), $("#times").text(c.times), !page.userPhone) {
              alert("\u60A8\u8FD8\u6CA1\u6709\u7ED1\u5B9A\u624B\u673A\u53F7\uFF0C\u8BF7\u5148\u7ED1" +
                "5B9A\u624B\u673A\u53F7");
              var d = window.location.href,
                f = encodeURIComponent(d),
                h = "http://weixin.unisk.cn/weixinVipController.do?bindingMobile&weixinid=gh_ad35a4a0" +
                "9b60&openid=" + page.openId + "&cburl=" + f;
              window.location.href = h
            }
            c.subscribe ?
              (c.prize && ($(".price-list li span").text(page.prizeName[c.prize]), $(".price-list li a").attr("data-id", c.prize).text("\u7ACB\u5373\u524D\u5F80")), page.changeCard()) :
              $("#wxFollow")
              .removeClass("none")
              .addClass("fadeIn")
          }
        })
    },
    checkUserFunc: function checkUserFunc() {
      var a = $.Deferred();
      return $.ajax({
          type: "get",
          url: page.host + "ruffleCard/getInfo",
          data: {
            openId: page.openId
          },
          success: function success(b) {
            a.resolve(b)
          },
          error: function error() {}
        }),
        a.promise()
    },
    changeCard: function changeCard() {
      var a = this,
        b = 0,
        c,
        d = "";
      $(a.card_chance).on("click", function (f) {
        if (f.stopPropagation(), b++, !a.play_game_times)
          return void $("#overTimes").removeClass("none").addClass("fadeIn");
        if (1 == b) {
          var g = Math.floor(3 * Math.random()) + 1;
          page
            .prizeType
            .map(function (h) {
              if (g == h.type) {
                $(f.target);
                c = h.imgUrl,
                  d = h.type
              }
            }),
            a.getCard($(this).children("a")[1], c),
            $(this).addClass("hover")
        } else
          2 == b && ($(this).attr("data-flag", "1"), $(a.card_chance).off(), b = 0, page.judgePrize(d, f.target))
      })
    },
    getCard: function getCard(a, b) {
      var c = this;
      c
        .cardGame
        .createCard(a, b),
        2 != c.clickCount && c.count++,
        (c.clickCount = 2) && (c.count = 0)
    },
    comCard: function comCard(a) {
      var b = this;
      b.play_game_times--,
        setTimeout(function () {
          a
            ?
            ($(b.game_result).css("display", "block").addClass("fadeIn"), $(b.medal_failed).css("display", "none"), $(b.medal_success).css("display", "block")) :
            ($(b.game_result).css("display", "block").addClass("fadeIn"), $(b.medal_failed).css("display", "block"), $(b.medal_success).css("display", "none")),
            $(b.times).html(b.play_game_times)
        }, 1500)
    },
    judgePrize: function judgePrize(a, b) {
      var c = this,
        d,
        f,
        g = [1, 2, 3];
      g.splice(a - 1, 1),
        $.ajax({
          type: "post",
          url: page.host + "ruffleCard/getAward",
          data: {
            openId: page.openId,
            prizeType: a
          },
          success: function success(h) {
            if (0 == h.code) {
              if (page.prizeType.map(function (k) {
                  a == k.type && (d = k.imgUrl)
                }), f = "./getImg/" + g[Math.floor(2 * Math.random())] + ".png", h.data.win) {
                c.getCard($(b).next(), d),
                  $(".price-list li span").text(page.prizeName[a]),
                  $(".price-list li a")
                  .attr("data-id", a)
                  .text("\u7ACB\u5373\u524D\u5F80"),
                  $(b)
                  .parent()
                  .addClass("hover");
                $(".medal-success").css({
                  background: "url('./img/tinified/" + [
                    "",
                    "prize1.png",
                    "prize2.png",
                    "prize3.png",
                    "prize4.png",
                    "prize5.png"
                  ][a] + "') no-repeat",
                  backgroundSize: "5.74rem 4.28rem"
                })
              } else
                c.getCard($(b).next(), f),
                $(b)
                .parent()
                .addClass("hover");
              page.comCard(h.data.win)
            }
          },
          error: function error() {}
        })
    },
    getPrizeFunc: function getPrizeFunc() {
      $("#getMyPrize")
        .on("click", function (a) {
          a.preventDefault();
          var b = $(this).attr("data-id");
          return 4 != b && page.hasAward ?
            void alert("\u60A8\u5DF2\u7ECF\u9886\u8FC7\u5956\u54C1\u4E86") :
            void(1 == b || 2 == b || 3 == b ?
              $("#addr_panel").removeClass("none").addClass("fadeIn") :
              4 == b ?
              ($("#youhui_panel").removeClass("none").addClass("fadeIn"), $.ajax({
                type: "post",
                url: page.host + "ruffleCard/receive",
                data: {
                  openId: page.openId
                },
                beforeSend: function beforeSend() {},
                completed: function completed() {},
                success: function success(c) {
                  0 == c.code && (page.hasAward = !0, page.youhuiquan = c.data.coupon, $("#youhuiquan").text(page.youhuiquan), location = location)
                },
                error: function error() {}
              })) :
              5 == b && $.ajax({
                type: "post",
                url: page.host + "ruffleCard/receive",
                data: {
                  openId: page.openId
                },
                beforeSend: function beforeSend() {},
                completed: function completed() {},
                success: function success(c) {
                  0 == c.code && (alert("\u9886\u53D6\u6210\u529F"), location = location)
                },
                error: function error() {}
              }))
        })
    },
    sendAddr: function sendAddr() {
      $("#sendAddrBtn")
        .on("click", function () {
          return $("#uname").val() ?
            checkMobile($("#uphone").val()) ?
            $("#uaddr").val() ?
            void $.ajax({
              type: "post",
              url: page.host + "ruffleCard/saveAddress",
              data: {
                openId: page.openId,
                username: $("#uname").val(),
                phone: $("#uphone").val(),
                address: $("#uaddr").val()
              },
              beforeSend: function beforeSend() {},
              completed: function completed() {},
              success: function success(a) {
                0 == a.code && (alert("\u9886\u53D6\u6210\u529F"), $("#addr_panel").hide(), location = location)
              },
              error: function error() {}
            }) :
            void alert("\u8BF7\u586B\u5199\u5730\u5740") :
            void alert("\u8BF7\u586B\u519911\u4F4D\u624B\u673A\u53F7") :
            void alert("\u8BF7\u586B\u5199\u59D3\u540D")
        })
    },
    showPrice: function showPrice() {
      var a = this;
      $(a.btn_price).click(function (b) {
        b.preventDefault(),
          MyToggle(a.game_ruler, "[data-toggle='1']", 500),
          page.rulerScroller()
      })
    },
    pageSwap: function pageSwap() {
      var a = this;
      $(a.back_main_btn).click(function (c) {
          c.preventDefault(),
            MyToggle(a.card_body, "[data-toggle='1']", 500)
        }),
        $(".closeAddr").click(function (c) {
          c.preventDefault(),
            $("#addr_panel")
            .addClass("none")
            .removeClass("fadeIn")
        }),
        $(".closeYouhui").click(function (c) {
          c.preventDefault(),
            $("#youhui_panel").addClass("none")
        }),
        $(a.invite_btn).click(function (c) {
          c.preventDefault(),
            $("#share").show()
        }),
        $(a.btn_medal).click(function (c) {
          c.preventDefault(),
            MyToggle(a.game_price, "[data-toggle='1']", 500)
        }),
        $(a.btn_ruler).click(function (c) {
          c.preventDefault(),
            MyToggle(a.game_madel, "[data-toggle='1']", 500)
        }),
        $(a.btn_close).click(function (c) {
          c.preventDefault();
          var d = "#" + $(this).attr("data-close");
          "#game_invite" == d && ($(".header").fadeIn(500), $(".footer").fadeIn(500)),
            MyToggle(a.card_body, d)
        }),
        $(a.btn_reload).click(function (c) {
          c.preventDefault(),
            MyToggle(a.card_body, a.game_result),
            a.changeCard(),
            $(a.card_chance)
            .removeClass("hover")
            .find(".card-face img")
            .remove()
        }),
        $(a.play_invite).click(function (c) {
          c.preventDefault(),
            a.changeCard(),
            $(a.card_chance).removeClass("hover"),
            $(a.card_chance)
            .removeClass("hover")
            .find(".card-face img")
            .remove()
        }),
        $(a.btn_play_again).click(function (c) {
          c.preventDefault(),
            MyToggle(a.card_body, a.game_result),
            a.changeCard(),
            $(a.card_chance)
            .removeClass("hover")
            .find(".card-face img")
            .remove()
        })
    },
    rulerScroller: function rulerScroller() {
      new iScroll("rulerWrapper", {
        useTransform: !1,
        vScroll: !0,
        hScroll: !1,
        hScrollbar: !1,
        checkDOMChanges: !0,
        hideScrollbar: !0,
        onBeforeScrollStart: function onBeforeScrollStart(b) {
          for (var c = b.target; 1 != c.nodeType;)
            c = c.parentNode;

          "SELECT" != c.tagName && "INPUT" != c.tagName && "TEXTAREA" != c.tagName && "A" != c.tagName && "IMG" != c.tagName && "li" != c.tagName && b.preventDefault()
        }
      })
    }
  };

function MyToggle(a, b, c) {
  $(b).fadeOut(0),
    $(a).fadeIn(c)
}
page.init();